<!-- sku管理 - 综合表单页面 -->
<template>
  <div class="page-container">
    <ex_super_table_page v-bind="option" ref="SuperTablePage">
      <template v-slot:action-right>
        <el-upload action="" :show-file-list="false" style="display: inline;" :http-request="uploadFile">
          <el-button slot="trigger" size="mini" type="primary">{{ $tt('label_import_excel') }}</el-button>
        </el-upload>
      </template>
    </ex_super_table_page>
    <ex_super_dialog_form ref="formDialog" :title="dialogTitle" v-bind="dialog"
      @afterSubmitSuccess="afterSubmitSuccess" />
  </div>
</template>
<script>

import superTablePageFunction from '@/mixin/super-table-page-function';
export default {
  title: 'label_sku_categ_manage',
  mixins: [superTablePageFunction],
  data() {
    return {
      // editObj: null,

      option: {
        search: {
          //搜索框内容，key为搜索字段，label为标签提示
          model: [
            { label: "label_categ_name", key: "name", type: 'input', attrs: { clearable: true } },//普通文字输入
            // { label: "品类级别", key: "level", type: 'select', enum: 'IgLevelEnum', attrs: { clearable: true } },//普通文字输入
            //所有元素均可添加可选attrs对象增加属性，详细属性列表由element-ui提供
          ],
        },

        action: [
          {
            label: this.$tt("label_add_level_categ", { level: 1 }), attrs: { icon: "el-icon-plus" },
            auth:'oms_sku_category_manage_edit',
            click: () => {
              this.dialogTitle = this.$tt("label_add_level_categ", { level: 1 })
              this['$refs']['formDialog'].show({ igPid: 0, igStatus: "ENABLE" }, { setData: true })
            }
          },
        ],
        actionExtra: [
          {
            label: 'label_refresh_categ', attrs: {type: "primary"},
            auth:'oms_sku_category_manage_edit',
            click: () => {
              this.$confirm(this.$tt('tip_confirm_refresh_categ'), this.$tt('label_tip'), {
                confirmButtonText: this.$tt('label_confirm'),
                cancelButtonText: this.$tt('label_cancel'),
                type: 'warning'
              }).then(() => {
                this.$ajax({
                  url: "/mms-api/sku/MmsSkuCategoryInfoRpc/refreshCacheAndFile.json",
                  success: (res) => {
                    this.$message({
                      type: 'success',
                      message: this.$tt('tip_operate_success')
                    });
                  }
                })

              }).catch(() => {
                this.$message({
                  type: 'info',
                  message: this.$tt('tip_has_cancel_refresh')
                });
              });
            }
          },
          { label: 'label_download_excel_import_temp', click: this.downloadImportTemp },
        ],

        table: {
          autoPage: false,
          'row-key': "id",
          'tree-props': { children: 'children', hasChildren: 'hasChildren' },

          url: '/mms-api/sku/MmsSkuCategoryInfoRpc/allCategory.json',
          urlDataHandle: (res) => {
            this.setLevel(res.data.rows, 1)
            return res.data.rows
          },
          model: [
            { key: 'name', label: 'label_categ_name', align: 'start' },
            {
              type: "operate", width: 200,
              operate: [
                {
                  label: (row) => this.$tt("label_add_level_categ", { level: row.level + 1 }),
                  auth:'oms_sku_category_manage_edit',
                  click: (row) => {
                    this.dialogTitle = this.$tt("label_add_level_categ", { level: row.level + 1 })
                    this['$refs']['formDialog'].show({ "igPid": row["id"], igStatus: "ENABLE", parentName: row.name }, { setData: true })
                  },
                  disabled: row =>{ return row.id === -1 || row.id === '-1'}
                },
                {
                  label: 'label_edit',
                  auth:'oms_sku_category_manage_edit',
                  click: (row) => {
                    this.dialogTitle = this.$tt("label_edit_level_categ", { level: row.level + 1 })
                    this['$refs']['formDialog'].show({ "id": row["id"] }, {
                      detailDataHandle: (data) => {

                        data.parentName = row.parentName
                        return data
                      }
                    })
                  },
                  disabled: row =>{ return row.id === -1 || row.id === '-1'}
                },

                {
                  label: 'label_delete',
                  auth:'oms_sku_category_manage_delete',
                  type: "confirm-button", url: '/mms-api/sku/MmsSkuCategoryInfoRpc/remove.json', message: '确定要删除该品类吗？',
                  buttonClass: "button_text_danger", confirmButtonType: 'danger',
                  params: (row) => { return { "id": row["id"] } }, done: ['tip', 'reload'],
                  disabled: row =>{ return row.id === -1 || row.id === '-1'}
                },
              ],
            }
          ],
        }

        ,

      },

      dialogTitle: '',
      dialog: {
        submitUrl: '/mms-api/sku/MmsSkuCategoryInfoRpc/saveUpdate.json',
        detailUrl: '/mms-api/sku/MmsSkuCategoryInfoRpc/detail.json',
        form: {
          //弹窗表单设置，设置方式与搜索框对象一致。
          model: [
            { header: "label_base_info", },
            // { key: 'igPid', label: '父ID', disabled: true, },
            { key: 'parentName', label: 'label_super_categ', disabled: true, if: (r, c, v) => !!v },
            // { key: 'igLevel', label: '品类级别', disabled: true, },
            { key: 'igName', label: 'label_categ_name', },
            { key: 'igNameEn', label: 'label_categ_name_en', },
            // { key: 'igStatus', label: '是否启用', type: 'select', enum: 'IsAvailableStatusEnum',disabled: true, },
          ],
          rules: {
            'igName': [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' }],
            'igNameEn': [{ required: true, message: this.$tt('tip_can_no_empty'),  trigger: 'blur' }],
          },
        }
      },
    };
  },
  methods: {
    setLevel(list, level, item) {
      list.forEach(it => {
        it.level = level
        it.parentName = item ? item.name : ''
        if (it.children) this.setLevel(it.children, level + 1, it)
      })
    },

    uploadFile(cfg) {
      this.$uploadFile({
        loading: true,
        url: this.$url['mms/categoryImportExcel'],
        // url: '/mms-api/sku/MmsSkuCategoryInfoRpc/importExcel.json',
        file: cfg.file,
        success: (res) => {
          this.$emitPage("oms>import_manage", 'reload')
          this.$confirm(this.$tt("tip_importing_confirm_goto_recode_list"), this.$tt('label_tip'), {
            type: 'success'
          }).then(() => {
            this.$router.push({
              path: '/page/oms/report/import_manage',
            });
          }).catch(() => {

          });
        }
      })
    },
    downloadImportTemp() {
      if (this.$clickFast()) return
      this.$downloadFile({
        loading: true,
        url: this.$url['mms/categoryDownloadImportTemp'],
        // url: '/mms-api/sku/MmsSkuCategoryInfoRpc/downloadImportTemp.json',
        window
      })
    },

    afterSubmitSuccess(data) {
      this.reload()
    },
  }
};

</script>
<style></style>
